# -*- coding: utf-8 -*-
from scpy.logger import get_logger
import os
import sys
import json
import pickle
from collections import Counter
import xlrd

reload(sys)
sys.setdefaultencoding('utf-8')

# logger = get_logger(__file__)
#
# MAPPING_FILE_NAME = 'A股和B股行业及地区分类_V0.3.xlsx'

CURRENT_PATH = os.path.dirname(__file__)
if CURRENT_PATH:
    CURRENT_PATH = CURRENT_PATH + "/"

with open(CURRENT_PATH+'industry_category.json','r') as fp:
    data = fp.read()
industry_category = json.loads(data)

def find_province_city(text):
    filename = 'resident_revised.pkl'
    pickleFile = file(CURRENT_PATH + filename,'r')
    mapping = pickle.load(pickleFile)
    pickleFile.close()
    province_dic = dict()
    city_dic = dict()
    for item in mapping['mapprovince'].keys():
        if text.find(item)!=-1:
            if mapping['mapprovince'][item] in province_dic.keys():
                province_dic[mapping['mapprovince'][item]] += 1
            else:
                province_dic[mapping['mapprovince'][item]] = 1
    provinceName = Counter(province_dic).most_common()[0][0]
    for item in mapping['mapcity'].keys():
        if text.find(item)!=-1:
            if mapping['mapcity'][item] in city_dic.keys():
                city_dic[mapping['mapcity'][item]] += 1
            else:
                city_dic[mapping['mapcity'][item]] = 1
    if len(city_dic)>=1:
        cityName = Counter(city_dic).most_common()[0][0]
    else:
        cityName = None
    return provinceName, cityName
#
# def find_mapping_list(filePath):
#     mapping_list = list()
#     codeList = list()
#     data = xlrd.open_workbook(filePath)
#     with open(CURRENT_PATH+'mapping') as f:
#         for line in f.readlines():
#             codeList.append(line.strip())
#     table = data.sheet_by_name(u'AB股行业及地区')
#     for i in range(1,table.nrows):
#         mapping_list.append({
#             'stockCode':codeList[i-1],
#             #'companyShortName':table.row_values(i)[2],
#             'category':table.row_values(i)[3],
#             'industry':table.row_values(i)[4],
#         })
#     return mapping_list
#
# MAPPING_LIST = find_mapping_list(MAPPING_FILE_NAME)
#
# if __name__ == '__main__':
#     # with open('industry_category.json','r') as fp:
#     #     data = fp.read()
#     # category_diction = json.loads(data)
#     # print category_diction
#     # provinceName, cityName = find_province_city(u'山东省济宁市高新区327国道58号')
#     # print provinceName, cityName
#     mapping_list = find_mapping_list(MAPPING_FILE_NAME)
#     print json.dumps(mapping_list,ensure_ascii=False,indent=1)